//
//  ViewController.m
//  CustomeTableViewCell
//
//  Created by Hoang Tuan on 12/12/12.
//  Copyright (c) 2012 Hoang Tuan. All rights reserved.
//

#import "ViewController.h"
#import "Film.h"
#import "CustomeTableViewCell.h"

@interface ViewController ()

@end
NSMutableData *receivedData;
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    mMutabArr = [[NSMutableArray alloc] init];

    NSArray *arrTitle = [[NSArray alloc] initWithObjects:@"Rambo",@"Kungfu",@"thanh long",@"chau tan", @"trieu vy", nil];
    NSArray * arrThumb = [[NSArray alloc] initWithObjects:@"rambo.jpeg",@"kungfu.jpeg",@"thanh-long.jpeg",@"chau-tan.jpeg",@"trieu-vy.jpeg", nil];
    int year[] = {2012,2011,2012,2010,2000};
    int rate[] = {8,7,9,8,8};
    int like[] = {1000, 3000, 2000,500,1000};
    [self connecServer];
    int n = arrTitle.count;
    for(int i=0; i<n; i++){
        Film *rambo = [[Film alloc] init];
        rambo.No = i+1;
        rambo.title = [arrTitle objectAtIndex:i];
        rambo.thumb = [arrThumb objectAtIndex:i];
        rambo.year = year[i];
        rambo.rate = rate[i];
        rambo.like = like[i];
        [mMutabArr addObject:rambo];
    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //CustomeTableViewCell *cell = [[CustomeTableViewCell alloc] init];
    CustomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
    
    Film *film = [mMutabArr objectAtIndex:indexPath.row];
    [cell.filmImgView setImage:[UIImage imageNamed:film.thumb]];
    [cell.filmTitleLbl setText:film.title];
    [cell.filmYearLbl setText:[NSString stringWithFormat:@"%d",film.year]];
    [cell.filmRateLbl setText:[NSString stringWithFormat:@"%d",film.rate]];
    [cell.filmLikeLbl setText:[NSString stringWithFormat:@"%d",film.like]];
    if(indexPath.row % 2 != 0){
        cell.contentView.backgroundColor = [self colorFromHexString:@"#F2F2F2"];
        [cell.imgSeparator setImage:[UIImage imageNamed:@"separator-gray.png"]];
    }else{
        [cell.imgSeparator setImage:[UIImage imageNamed:@"separator-white.png"]];
    }

    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [mMutabArr count];
}
-(UIColor *)colorFromHexString:(NSString *)hexString {
    unsigned rgbValue = 0;
    NSScanner *scanner = [NSScanner scannerWithString:hexString];
    [scanner setScanLocation:1]; // bypass '#' character
    [scanner scanHexInt:&rgbValue];
    return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Film *film = [mMutabArr objectAtIndex:indexPath.row];
    NSLog(@"%@",film.title);
}
-(void)connecServer
{
    // Create the request.
    NSLog(@"connectServer");
    
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                              
                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                              
                                          timeoutInterval:60.0];
    
    // create the connection with the request
    
    // and start loading the data
    
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    
    if (theConnection) {
        
        // Create the NSMutableData to hold the received data.
        
        // receivedData is an instance variable declared elsewhere.
        
        receivedData = [[NSMutableData data] retain];
        
    } else {
        
        // Inform the user that the connection failed.
        
    }
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{
    NSLog(@"did receive response");
    // This method is called when the server has determined that it
    
    // has enough information to create the NSURLResponse.
    
    
    
    // It can be called multiple times, for example in the case of a
    
    // redirect, so each time we reset the data.
    
    
    
    // receivedData is an instance variable declared elsewhere.
    
    [receivedData setLength:0];
    
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{
    NSLog(@"did receive data: %d", data.length);
    // Append the new data to receivedData.
    
    // receivedData is an instance variable declared elsewhere.
    
    [receivedData appendData:data];
    
}
- (void)connection:(NSURLConnection *)connection

  didFailWithError:(NSError *)error

{
    // release the connection, and the data object
    
    [connection release];
    
    // receivedData is declared as a method instance elsewhere
    
    [receivedData release];
    
    
    
    // inform the user
    
    NSLog(@"did fail with error - %@ %@",
          
          [error localizedDescription],
          
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    // do something with the data
    
    // receivedData is declared as a method instance elsewhere
    
    NSLog(@"connection did finish loading! Received %d bytes of data",[receivedData length]);
    
    
    
    // release the connection, and the data object
    
    [connection release];
    
    [receivedData release];
    
}
//-(NSCachedURLResponse *)connection:(NSURLConnection *)connection
//
//                 willCacheResponse:(NSCachedURLResponse *)cachedResponse
//
//{
//    NSLog(@"connection will cache response");
//    NSCachedURLResponse *newCachedResponse = cachedResponse;
//    
//    
//    
//    if ([[[[cachedResponse response] URL] scheme] isEqual:@"https"]) {
//        
//        newCachedResponse = nil;
//        
//    } else {
//        
//        NSDictionary *newUserInfo;
//        
//        newUserInfo = [NSDictionary dictionaryWithObject:[NSCalendar alloc]
//                       
//                                                  forKey:@"Cached Date"];
//        
//        newCachedResponse = [[[NSCachedURLResponse alloc]
//                              
//                              initWithResponse:[cachedResponse response]
//                              
//                              data:[cachedResponse data]
//                              
//                              userInfo:newUserInfo
//                              
//                              storagePolicy:[cachedResponse storagePolicy]]
//                             
//                             autorelease];
//        
//    }
//    
//    return nil;
//    
//}
@end
